/*
 * Copyright (c) 2023, LXMS (lxinet.cn).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.lxinet.lxms.generator.domain;

public class Result {

    public static final String SUCCEED_CODE = "SUCCEED";
    public static final String FAILED_CODE = "FAILED";

    private String code;
    private String message;
    private String detail;
    private Object data;

    /**
     * 返回正确结果
     *
     * @return
     */
    public static Result success() {
        Result result = new Result();
        result.setCode(SUCCEED_CODE);
        return result;
    }

    /**
     * 返回正确结果
     *
     * @param obj 返回数据
     * @return
     */
    public static Result success(Object obj) {
        Result result = new Result();
        result.setCode(SUCCEED_CODE);
        result.setData(obj);
        return result;
    }

    /**
     * 返回错误结果
     *
     * @return
     */
    public static Result error() {
        Result result = new Result();
        result.setCode(FAILED_CODE);
        return result;
    }

    /**
     * 返回错误结果
     *
     * @param message 错误信息
     * @return
     */
    public static Result error(String message) {
        Result result = new Result();
        result.setCode(FAILED_CODE);
        result.setMessage(message);
        return result;
    }

    /**
     * 返回错误结果
     *
     * @param message 错误信息
     * @param detail  详细信息
     * @return
     */
    public static Result error(String message, String detail) {
        Result result = new Result();
        result.setCode(FAILED_CODE);
        result.setMessage(message);
        result.setDetail(detail);
        return result;
    }

    /**
     * 结果是否错误
     *
     * @return
     */
    public boolean errored() {
        if (this == null || this.getCode().equals(Result.FAILED_CODE)) {
            return true;
        }
        return false;
    }

    /**
     * 结果是否成功
     *
     * @return
     */
    public boolean succeed() {
        if (this != null && this.getCode().equals(Result.SUCCEED_CODE)) {
            return true;
        }
        return false;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
